package com.litbw.user;


import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.util.Observable;
import java.util.Observer;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

import com.litbw.publicobservable.DraftCreatedMessage;

/**
 * 
 * @author Derrick Mink
 * @edited Trygve Stageberg
 *
 */
public class Draft extends Observable {
	JDialog frame;
	private DraftMakerPanel draft;
	private BufferedImage image;
	private User owner;

	@SuppressWarnings("serial")
	public Draft() {		
	}
	
	public void setUser(User user){
		owner = user;
	}
	
	public void showDraftMaker(Observer obs){
		Frame owner = null;
		frame = new JDialog(owner, true);
		draft = new DraftMakerPanel();
		addObserver(obs);
		frame.setLayout(new BorderLayout());
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.add(draft, BorderLayout.CENTER);
		JButton finished = new JButton("Finished");
		finished.addActionListener(new AbstractAction() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				image = new BufferedImage(draft.getWidth(), draft.getHeight(), BufferedImage.TYPE_INT_ARGB);
				draft.paint(image.getGraphics());
				
				setChanged();
				notifyObservers(new DraftCreatedMessage());
				
				frame.dispose();
			}

		});
		frame.add(finished, BorderLayout.SOUTH);
		frame.setTitle("Draft Creator");
		frame.pack();
		frame.setVisible(true);
	}

	public BufferedImage getImage() {
		/*BufferedImage bi = new BufferedImage(draft.getWidth(),
				draft.getHeight(), BufferedImage.TYPE_INT_ARGB);
		draft.paint(bi.getGraphics());*/
	
		return image;
	}
	
	public void setImage(final BufferedImage newImg) {
		image = newImg;
	}
}
